//
//  FDTabBarController.m
//  FD
//
//  Created by Yasin on 2018/10/10.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDTabBarController.h"

#import "HomeViewController.h"
#import "FindViewController.h"
#import "OrderViewController.h"
#import "MineViewController.h"
#import "ShangPinLeiMuViewController.h" //发现修改为:商品类目
#import "FDNavigationViewController.h" //全局替换系统导航条返回按钮

@interface FDTabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@end

@implementation FDTabBarController

+ (instancetype)shareFDTabarVC {
    
    static FDTabBarController *shareTabbarVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
        shareTabbarVC = [[FDTabBarController alloc] init];
    });
    
    return shareTabbarVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //修改
    [self setupSubController];
    
    //解决选中tabar 的item的颜色始终为蓝色的问题
    [self setupItemTitleTextAttribute];
    
    //iOS 解决UITabBarItem选中图片默认为蓝色的方法 找到图片，直接修改：Render As 为 Original Image
    
//    self.tabBar
    self.delegate = self;
}


- (void)setupSubController {
    
    //适配国际化字段
    
    NSString *home = NSLocalizedString(TabHome, @"首页");
    NSString *find = NSLocalizedString(TabFind, @"点餐"); //发现-->点餐
    NSString *order = NSLocalizedString(TabOrder, @"订单");
    NSString *mine = NSLocalizedString(TabMine, @"我的");
    
    //首页有导航条，默认隐藏
    [self setupOneChildViewControler:[[HomeViewController alloc] init] title:home imageStr:@"home" selectedImageStr:@"homeSelected" haveNav:YES];
    
    //发现 有导航条
    [self setupOneChildViewControler:[[FindViewController alloc] init] title:find imageStr:@"faxian" selectedImageStr:@"faxianSelected" haveNav:YES];
    self.viewControllers[1].tabBarItem.tag = 1; //标记tag值
    
    [self setupOneChildViewControler:[[OrderViewController alloc] init] title:order imageStr:@"dindan" selectedImageStr:@"dindanSelected" haveNav:YES];
    
    [self setupOneChildViewControler:[[MineViewController alloc] init] title:mine imageStr:@"wode" selectedImageStr:@"wodeSelected" haveNav:YES];
}

- (void)setupOneChildViewControler:(UIViewController *)vc title:(NSString *)title imageStr:(NSString *)imageStr selectedImageStr:(NSString *)selectedImgStr haveNav:(BOOL)hasNav{
    
    //设置底部tabbar的 文字、图片、选中图片
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:imageStr];
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImgStr];
    [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImgStr];
    
    //添加子视图控制器
    if (hasNav) { //有导航条
        FDNavigationViewController *navVC = [[FDNavigationViewController alloc] initWithRootViewController:vc];
        [self addChildViewController:navVC];
    }else { //没有导航条
        [self addChildViewController:vc];
    }
}

- (void)setupItemTitleTextAttribute {
    UITabBarItem *item = [UITabBarItem appearance]; //只要有这个标志的 UI_APPEARANCE_SELECTOR 都可以统一设置
    
    //普通状态下文字颜色
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    //选中状态下文字颜色
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}


//- (void)setupItemTitleTextAttributefdfd  {
//    UITabBarItem *item = [UITabBarItem appearance]; // 只要有这个标志 UI_APPEARANCE_SELECTOR 都可以统一设置
//
//    // 设置普通状态下文字属性
//    NSMutableDictionary *normatAttrs = [NSMutableDictionary dictionary];
//    normatAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//    normatAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
//    [item setTitleTextAttributes:normatAttrs forState:UIControlStateNormal];
//
//    // 选中状态下文字属性
//    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
//    selectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//    selectAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
//    [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
//}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (item.tag == 1) {
        [self haveBug];
    }
}

- (void)haveBug2 {
    //拿到首页
    HomeViewController *homeVC =  self.childViewControllers[0];
    
    //push
    ShangPinLeiMuViewController *splmVC = [[ShangPinLeiMuViewController alloc] init];
    FDNavigationViewController *navVC = [[FDNavigationViewController alloc] initWithRootViewController:splmVC];
    [homeVC presentViewController:navVC animated:YES completion:nil];
}

- (void)haveBug {
    //model出商品类目控制器
    ShangPinLeiMuViewController *splmVC = [[ShangPinLeiMuViewController alloc] init];
    //要使用FDNavigationViewController作为导航控制器去进行管理。
    FDNavigationViewController *navVC = [[FDNavigationViewController alloc] initWithRootViewController:splmVC];
    [self presentViewController:navVC animated:YES completion:nil];
}

- (void)backAction {
    FDLog(@"触发返回按钮");
}

@end
